//
//  HNSH_ChatSendRedViewController.m
//  NongYe
//
//  Created by 李宇廷 on 2023/2/6.
//

#define kDesFieldPlaceholder @"兔年如意，一路长虹"

#import "HNSH_ChatSendRedViewController.h"
#import "HNSH_ChatSendRedInputView.h"

#import "HNSH_ChatReceiveRedViewController.h"

#import "ST_PayPasswordView.h"           //支付弹窗

@interface HNSH_ChatSendRedViewController ()

/* rightItemButton */
@property (nonatomic, strong) UIButton *rightItemButton;
/* countView */
@property (nonatomic, strong) HNSH_ChatSendRedInputView *countView;
/* moneyView */
@property (nonatomic, strong) HNSH_ChatSendRedInputView *moneyView;
/* desField */
@property (nonatomic, strong) UITextField *desField;
@end

@implementation HNSH_ChatSendRedViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"发绿色能量红包" showLeftItem:YES];
    [self.view addSubview:self.rightItemButton];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
    [self configUI];
}

- (void)addAllViews {
    
}

- (void)addAllLayout {
    
}

- (void)configUI {
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 16+kNavBarAndStatusBarHeight, 200, 20)];
    titleLabel.textColor = [UIColor colorWithHexString:@"#DF7302"];
    titleLabel.text = @"拼手气红包";
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:titleLabel];
    
    //红包个数
    HNSH_ChatSendRedInputView *countView = [[HNSH_ChatSendRedInputView alloc]initWithFrame:CGRectMake(0, 46+kNavBarAndStatusBarHeight, kScreenWidth, 79)];
    countView.imgV.image = [UIImage imageNamed:@"chat_sned_icon_hongbao"];
    countView.titleLabel.text = @"红包个数";
    countView.desLabel.text = @"个";
    countView.textField.placeholder = @"填写红包个数";
    countView.textField.keyboardType = UIKeyboardTypeNumberPad;
    countView.bottomLabel.text = [NSString stringWithFormat:@"本群共%ld人",self.memberCount];
    [self.view addSubview:countView];
    self.countView = countView;
    
    //红包金额
    HNSH_ChatSendRedInputView *moneyView = [[HNSH_ChatSendRedInputView alloc]initWithFrame:CGRectMake(0, 141+kNavBarAndStatusBarHeight, kScreenWidth, 79)];
    moneyView.imgV.image = [UIImage imageNamed:@"chat_sned_icon_pin"];
    moneyView.titleLabel.text = @"总金额";
    moneyView.desLabel.text = @"绿色能量";
    moneyView.textField.placeholder = @"填写总金额";
    moneyView.textField.keyboardType = UIKeyboardTypeDecimalPad;
    moneyView.bottomLabel.text = [NSString stringWithFormat:@"绿色能量余额：%.2f",[[ST_UserManager shareInstance].user.integral doubleValue]];
    [self.view addSubview:moneyView];
    self.moneyView = moneyView;
    
    //描述
    UIView *desView = [[UIView alloc]initWithFrame:CGRectMake(16, 236+kNavBarAndStatusBarHeight, kScreenWidth-32, 52)];
    desView.backgroundColor = [UIColor whiteColor];
    desView.layer.cornerRadius = 8.0f;
    desView.layer.masksToBounds = YES;
    [self.view addSubview:desView];
    UITextField *desField = [[UITextField alloc]initWithFrame:CGRectMake(16, 0, desView.width-32, 52)];
    desField.font = [UIFont systemFontOfSize:16];
    desField.textColor = [UIColor colorWithHexString:@"#333333"];
    desField.keyboardType = UIKeyboardTypeDecimalPad;
    desField.placeholder = kDesFieldPlaceholder;
    [desView addSubview:desField];
    self.desField = desField;
    
    
    //红包金额
    UILabel *moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, 357+kNavBarAndStatusBarHeight, kScreenWidth-32, 50)];
    moneyLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    moneyLabel.text = @"0.00绿色能量";
    moneyLabel.font = [UIFont boldSystemFontOfSize:36];
    [self.view addSubview:moneyLabel];
    moneyLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:moneyLabel.text SelectContent:@"绿色能量" CommonFont:moneyLabel.font SpeFont:[UIFont systemFontOfSize:16] Color:moneyLabel.textColor];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    
    //改变金额
    [[moneyView.textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *field = x;
        NSLog(@"输入==>%@",field.text);
        moneyLabel.text = [NSString stringWithFormat:@"%.2f绿色能量",[field.text doubleValue]];
        moneyLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:moneyLabel.text SelectContent:@"绿色能量" CommonFont:moneyLabel.font SpeFont:[UIFont systemFontOfSize:16] Color:moneyLabel.textColor];
        moneyLabel.textAlignment = NSTextAlignmentCenter;
    }];
    
    
    //发送红包
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenWidth/2-115, 427+kNavBarAndStatusBarHeight, 230, 44);
    [button setTitle:@"放入红包" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor colorWithHexString:@"#F9624A"];
    button.layer.cornerRadius = 4;
    button.layer.masksToBounds = YES;
    
    __weak __typeof(&*self)weakSelf = self;
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (![countView.textField.text isNotBlank] ||
            [countView.textField.text integerValue] <= 0) {
            [self.view makeToast:@"红包个数不正确"];
            return;
        }
        if ([countView.textField.text integerValue] > self.memberCount) {
            [self.view makeToast:@"红包个数不能大于群成员数"];
            return;
        }
        if (![moneyView.textField.text isNotBlank] ||
            [moneyView.textField.text doubleValue] <= 0) {
            [self.view makeToast:@"红包金额不正确"];
            return;
        }
        if (([moneyView.textField.text doubleValue] / [countView.textField.text integerValue]) < 0.01 ) {
            [self.view makeToast:@"红包金额人均不小于0.01"];
            return;
        }
        [weakSelf paySendRed];
    }];
}

- (UIButton *)rightItemButton {
    if (!_rightItemButton) {
        _rightItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightItemButton.frame = CGRectMake(kScreenWidth-88, kStatusBarHeight, 88, 44);
        [_rightItemButton setTitle:@"红包记录" forState:UIControlStateNormal];
        [_rightItemButton setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        _rightItemButton.titleLabel.font = [UIFont systemFontOfSize:14];
        __weak __typeof(&*self)weakSelf = self;
        [[_rightItemButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            HNSH_ChatReceiveRedViewController *vc = [[HNSH_ChatReceiveRedViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _rightItemButton;
}

//MARK: 支付
- (void)paySendRed {
    [ST_PayPasswordView showPasswordViewWithSureBlock:^(NSString * _Nonnull password) {
        [self loadSendRedWithPwd:password];
    } cancelBlock:^{
        
    }];
}

//MARK: network
//- (void)loadSendRedWithPwd:(NSString *)pwd {
//    [CFBNetworkHelper getDataWithPathUrl:URL_Task_Config params:@{} from:YES needToken:YES successBlock:^(BOOL result, NSString * _Nonnull message, NSDictionary * _Nonnull dic) {
//        if (result) {
//            NSArray *ary = [NSArray yy_modelArrayWithClass:NYTaskHomeLandModel.class json:dic[@"data"]];
//            self.landView.landAry = ary;
//        }
//    } failBlock:^(NSError * _Nonnull error) {
//
//    }];
//}

- (void)loadSendRedWithPwd:(NSString *)pwd {
    [HNSH_ChatNetServer requestRongCloudRedPacketSendWithGroupId:self.targetId pay_password:pwd title:self.desField.text quantity:self.countView.textField.text amount:self.moneyView.textField.text success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"发送成功"];
            NSString *desString = [NSString stringWithFormat:@"%@",responseObject.resultset[@"title"]];
            NSString *redId = [NSString stringWithFormat:@"%@",responseObject.resultset[@"id"]];
            if (self.sendRedBlock) {
                self.sendRedBlock(self.countView.textField.text, self.moneyView.textField.text,desString,redId);
            }
            
            [[ST_UserManager shareInstance] loadUserInfoComplate:^{
                
            }];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
